<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/17
 * Time: 15:22
 */

namespace zhtfrontend\controllers;


use zhtfrontend\components\bases\BaseController;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\Comments;
use zhtfrontend\models\datas\CommentInfo;
use yii\data\Pagination;
use zhtfrontend\models\datas\CommentRelation;

class EvaluatelistController extends BaseController
{

    //public $layout='@app/views/layouts/iframe.php';

    /*
     * 评价列表
     * */
    public function actionIndex()
    {
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $comment = Comments::find()->orderBy('id DESC');
        $pages = new Pagination(['totalCount' => $comment->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $comment = $comment->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($comment, ['operate', 'valuator_name', 'accepter_name']);

        return $this->render('index', ['data' => $array, 'pages' => $pages]);
    }

    /**
     * 评分
     */

    public function actionView()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        $id = \Yii::$app->request->get('id');
        //查询已知comment_id的comment_relation
        $comment_relation = CommentRelation::find()->where(['comment_id' => $id])->asArray()->all();
        $data = [];
        foreach ($comment_relation as $k => $v) {
            //遍历每条relation 查到相应的评价项及评分
            $comment_info = CommentInfo::findOne(['id' => $v['comment_info_id']]);
            $data[$k] = [
                'comment_name' => $comment_info->title,
                'comment_value' => $v['for_value'],
            ];
        }
        return $this->render('view', ['data' => $data]);
    }
}
